package cn.sirun.com.fca.account.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.presenter.IPresenter;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;
import com.tencent.cloud.uikit.widget.edittext.LineTwoEditText;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.model.base.CarInfoDetailRequest;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.base.SubmitPhoneResponse;
import cn.sirun.com.fca.account.model.bind.RecognizePicResponse;
import cn.sirun.com.fca.account.model.bind.Reseller;
import cn.sirun.com.fca.account.model.bind.SubmitCarAuthenticationRequest;
import cn.sirun.com.fca.account.model.bind.SubmitCarAuthenticationResponse;
import cn.sirun.com.fca.account.model.bind.SubmitPersonalInfoRequest;
import cn.sirun.com.fca.account.model.bind.SubmitPersonalInfoResponse;
import cn.sirun.com.fca.account.presenter.ICarBelongtoAuthPresenter;
import cn.sirun.com.fca.account.presenter.impl.CarBelongtoAuthPresenter;
import cn.sirun.com.fca.account.uitl.AccountStatusUtils;
import cn.sirun.com.fca.account.view.ICarBelongtoAuthView;

public class CarBelongtoAuthFlowView extends BaseLoadingFlowView<ICarBelongtoAuthPresenter> implements ICarBelongtoAuthView {
    private ImageView mNameIV, mPhoneIV, mIdentityIV;
    private TextView mNameTV, mPhoneTV, mIdentityTV, mSexTV,mNextTV,mCarOwnerName,mCarVin;
    private LineEditText mNameET, mPhoneET, mIdentityET;
    private LinearLayout mSexLL;
    private View mNameLineV, mPhoneLineV, mIdentivyLineV;


    private RecognizePicResponse mRecognizePicResponse;

    private String mDin,vin;

    public CarBelongtoAuthFlowView(Activity activity) {
        super(activity);
    }

    public CarBelongtoAuthFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    protected ICarBelongtoAuthPresenter createPresenter() {
        return new CarBelongtoAuthPresenter();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        final IovToolbar toolbar = SystemUiUtils.showActionBar(
                activity, "");
        toolbar.setHomeAsUpText(R.string.tv_title_upback);
    }

    private void initView() {
        setContentView(R.layout.owner_auth_belongto_car_activity);
        final Activity activity = getActivity();
        mNameIV = activity.findViewById(R.id.iv_name_prompt);
        mPhoneIV = activity.findViewById(R.id.iv_phone_prompt);
        mIdentityIV = activity.findViewById(R.id.iv_identity);

        mNameTV = activity.findViewById(R.id.tv_name);
        mPhoneTV = activity.findViewById(R.id.tv_phone);
        mIdentityTV = activity.findViewById(R.id.tv_identity);
        mSexTV = activity.findViewById(R.id.tv_sex);

        mNameET = activity.findViewById(R.id.et_name);
        mPhoneET = activity.findViewById(R.id.et_phone);
        mIdentityET = activity.findViewById(R.id.et_identity);

        mSexLL = activity.findViewById(R.id.ll_sex);

        mNameLineV = activity.findViewById(R.id.v_name);
        mPhoneLineV = activity.findViewById(R.id.v_phone_line);
        mIdentivyLineV = activity.findViewById(R.id.v_identity);

        mCarOwnerName = activity.findViewById(R.id.et_owner);
        mCarVin = activity.findViewById(R.id.et_vin);

        mNextTV = activity.findViewById(R.id.tv_next);

        mNextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /**
                 * 提交用户信息成功后，进行车辆认证
                 */
                checkData();


            }
        });


        Intent intent = getActivity().getIntent();
        mRecognizePicResponse = (RecognizePicResponse) intent.getSerializableExtra("personinfo");
        mDin = intent.getStringExtra("din");

        if(mRecognizePicResponse != null){
            mNameET.setText(mRecognizePicResponse.getName());
            mIdentityET.setText(mRecognizePicResponse.getId());
            mSexTV.setText(mRecognizePicResponse.getSex());
            mCarOwnerName.setText(mRecognizePicResponse.getName());
        }



        CarInfoDetailRequest request = new CarInfoDetailRequest();
        request.setDin(mDin);
        getPresenter().getCarInfoDetail(request);

    }


    private void checkData(){

        String nameOwner = mCarOwnerName.getText().toString();
        String nameEt = mNameET.getText().toString();
        String identityET = mIdentityET.getText().toString();
        String sexTv = mSexTV.getText().toString();
        if(nameOwner != null && nameEt != null && identityET != null && sexTv != null){

            SubmitPersonalInfoRequest request = new SubmitPersonalInfoRequest();
            request.setName(mNameET.getText().toString());
            request.setId(mIdentityET.getText().toString());
            request.setSex(mSexTV.getText().toString());
            request.setIdType("1");
            request.setAddress("地址");
            getPresenter().submitPersonalInfo(request);
        }else{
            AccountStatusUtils.showToast(getActivity(), 0, "有未填项，请检查！");
        }


    }

    @Override
    public void getCarInfoDetail(BaseResponse<CarInfoDetailResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            if(response.getData().getVin() != null){
                vin = response.getData().getVin();
                mCarVin.setText(vin);
            }

        }else{

        }
    }

    @Override
    public void submitPersonalInfo(BaseResponse<SubmitPersonalInfoResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {

            SubmitCarAuthenticationRequest request = new SubmitCarAuthenticationRequest();
            Reseller reseller = new Reseller();
            reseller.setResellerProvince("xxx");
            reseller.setResellerName("xxx");
            reseller.setResellerCity("xxx");

            request.setDin(mDin);
            request.setCarOwnerName("薛乔铭");
            request.setDrivingLicenseRegTime("xxx");
            request.setEngineId("xxx");
            request.setInsuranceCompany("xxx");
            request.setPlateNumber("xxx");
            request.setReseller(reseller);
            request.setVin(vin);
            getPresenter().submitCarAuthentication(request);
        }else{

        }
    }

    @Override
    public void submitCarauthentication(BaseResponse<SubmitCarAuthenticationResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {

            if(response.getData() != null && response.getData().getAuthenticationStatus() != null){
                String authenticationStatus = response.getData().getAuthenticationStatus();

                if(authenticationStatus.equals("1")){
                    Intent intent = Router.fromPath(AccountActivityConst.Path.OWNER_SETTING_INITAL_PIN);
                    ActivityUtils.startActivity(getActivity(), intent);
                }else{

                }

            }


        }else{

        }
    }
}
